#include <iostream>

#include <vector>
#include <thread>
#include <atomic>

class CTLSData
{
public:
    CTLSData(int iID, int iVal)
    {
        m_iID = iID;
        m_iValue = iVal;
        //printf("%s: %d, %d\n", __FUNCTION__, m_iID, m_iValue);
        //std::cout << __FUNCTION__ << ", " << m_iID << ", " << m_iValue << std::endl;
    }

    ~CTLSData()
    {
        //printf("%s: %d, %d\n", __FUNCTION__, m_iID, m_iValue);
        //std::cout << __FUNCTION__ << ", " << m_iID << ", " << m_iValue << std::endl;
    }
private:
    int m_iID;
    int m_iValue;
};


class CAppTLS
{
public:
    void Run()
    {
        printf("Start running\n");
        std::vector<std::thread> vecThreads;
        for (int i = 0; i < 10; i++)
        {
            vecThreads.push_back(std::thread([&](int i){
                //printf("Thread %d is running\n", i);
                this->GetTLSData(i);
                //printf("Thread %d will exit\n", i);
            }, i)
            );
        }

        for (auto& t : vecThreads)
        {
            t.join();
        }

        printf("End running\n");
    }
private:
    void GetTLSData(int iID)
    {
        static std::atomic<int> objValue = {0};
        auto begin = clock();
        static thread_local CTLSData objTLSData(iID, ++objValue);
        auto end = clock();
        printf("Elapsed %lu, %f\n", end - begin, (end - begin) * 1.0/CLOCKS_PER_SEC);
    }
};

int main(int argc, char** argv)
{
    CAppTLS objApp;

    objApp.Run();

    return 0; 
}